<?php
include "../config.php";
$Login  = new Login();
$Login->verificaLogin(true);
$Usuario = new Usuario();
$Usuario = unserialize($_SESSION["usuario"]);
$Tarefa = new Tarefas();

$action = (isset($_REQUEST["action"])) ? $_REQUEST["action"] : "listar";

switch ($action){
	case "listar":
		$titulo="A fazer";
		$categorias = $Tarefa->selectAllCategoria();
		$tarefas =  $Tarefa->selectTarefasfaNaoFeitas();
		$html = "html/tarefas/lista.php";
		break;
	case "prontas":
		$titulo="Prontas";
		$categorias = $Tarefa->selectAllCategoria();
		
		$tarefas =  $Tarefa->selectTarefasfaNaoFeitas(1);
		$html = "html/tarefas/prontas.php";
		break;
	case "categoria":
		$titulo="Categoria ";
		if(isset($_GET["id"])){
			$categorias = $Tarefa->selectAllCategoria();
			$tarefas =  $Tarefa->selectTarefasfaNaoFeitas($_GET["status"],$_GET["id"]);
			$html = "html/tarefas/lista.php";
		}
		break;
	case "concluida":
		if(isset($_POST["id"])){
			 $status = $Tarefa->statusTarefa($_POST["id"]);
			 $troca =($status == 0) ? 1:0;
			 if($Tarefa->updateTarefaConcluida($_POST["id"],$troca)){
			 	if((int)$status == 1){
			 		$Tarefa->removeTarefaUsuarioFeita($_POST["id"], $Usuario->getId());
			 		echo "2";
			 	}
			 	else{
			 		$Tarefa->insereTarefaUsuarioFeita($_POST["id"], $Usuario->getId());
			 		echo "1";
			 	}
			 }
			 else{
			 	echo "0";
			 }
		}
		else{
			echo "0";
		}
		exit;
		break;		
	case "lidas":
		if($Tarefa->temTarefaNaoFeitas()){
			echo "1";
		}
		else{
			echo "0";
		}
		exit;
		break;
	case "nova":
		if(isset($_POST["titulo"])){
			
			$insert = $Tarefa->novaTarefa($_POST["titulo"]);
			if($insert){
				if(isset($_POST["id"])){
					$Tarefa->marcaTarefaLida($_POST["id"]);
				}
				echo "Salvo";
			}
			else{
				echo "Ocorreu algum erro!";
			}
		}
		exit;
		break;
	case "buscar":
		$titulo="A fazer";
		if(isset($_GET["q"]) && strlen($_GET["q"]) > 2){
			$tarefas =  $Tarefa->serch($_GET["q"]);
		}
		else{
			$tarefas =false;
		}
		$html = "html/tarefas/lista.php";
		break;
	default:
		$categorias = $Tarefa->selectAllCategoria();
		$titulo="A fazer";
		$categorias = $Tarefa->selectAllCategoria();
		$tarefas =  $Tarefa->selectTarefasfaNaoFeitas();
		$html = "html/tarefas/lista.php";
		break;
}

?>

<!DOCTYPE HTML>
<html lang="pt-br">
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="estilo.css">
	<title><?php echo $titulo;?></title>
	<?php include DIRVIEWS."html/topo.html"?>
    <style type="text/css">
      body {
        background-color: #f5f5f5;
      }

    input#customId{
   background: #888 !important;
   border: none !important;
   color: #000 !important;
}
    </style>
</head>
<body>
<?php include DIRVIEWS."html/menu.php"?>

<?php include DIRVIEWS."".$html;?>

 <!-- Button trigger modal -->

  <!-- Modal -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title">Nova tarefa</h4>
        </div>
        <div class="modal-body">
          Carregando ...
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal" id="cancela">Cancelar</button>
          <button type="button" class="btn btn-primary" id="salvar">Savar</button>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->

<?php include DIRVIEWS."html/rodape.html"?>
<script type="text/javascript" src="/views/js/tarefas.js"></script>
<script type="text/javascript">
<!--

//-->
</script>

</body>
</html>
